<template>
	<view class="mine-evaluate-edit" v-if="detail.id">
		<uni-nav-bar :fixed="true" :shadow="false" :statusBar="true" color="#fff"
			:backgroundColor="scrollTop < 20 ?``:primaryColor" leftIcon="icon-left" title="订单评价">
		</uni-nav-bar>
		<view :style="{height:`${configInfo.navBarHeight}px`}"></view>
		<image mode="aspectFill" lazy-load class="common-bg abs" src="https://lbqny.migugu.com/admin/farm/bg-cash.png"></image>
		<view class="flex-column mt-md ml-lg mr-lg pd-lg fill-base radius-24">
			<block v-if="options.type == 'shop'">
				<view class="order-item flex-center fill-base" :class="[{'mt-md':index!=0}]"
					v-for="(item,index) in detail.order_goods" :key="index">
					<image mode="aspectFill" lazy-load class="avatar radius-34" :src="item.goods_cover"></image>
					<view class="flex-1 ml-lg">
						<view class="f-title c-title text-bold ellipsis" style="max-width: 470rpx;">
							{{item.goods_name}}
						</view>
						<view class="flex-between f-caption c-caption mt-sm">
							<view class="ellipsis" :style="{maxWidth:item.refund_num*1>0?'300rpx':'470rpx'}">
								{{item.spe_name}}
							</view>
							<view class="flex-1 c-warning text-right" v-if="item.refund_num*1>0">
								已退x{{item.refund_num}}
							</view>
						</view>
						<view class="flex-y-baseline mt-sm">
							<view class="flex-1 f-paragraph c-warning">¥{{item.goods_price}} </view>
							<view class="f-caption c-caption">x {{item.goods_num}} </view>
						</view>
					</view>
				</view>
			</block>
			<view class="order-item flex-center fill-base" v-if="options.type != 'shop'">
				<image mode="aspectFill" lazy-load class="avatar radius-34" :src="detail.goods_cover"></image>
				<view class="flex-1 ml-lg">
					<view class="f-title c-title text-bold ellipsis" style="max-width: 470rpx;">
						{{detail.goods_name}}
					</view>
					<block v-if="options.type == 'claim'">
						<view class="title f-caption c-caption mt-sm"> 数量：{{`${detail.num}/${detail.unit}`}} </view>
						<view class="title f-caption c-caption"> 配送周期：{{detail.send_cycle}} </view>
					</block>
					<block v-if="options.type == 'land'">
						<view class="f-caption c-caption mt-sm"> 租赁期限：{{detail.end_time}} 到期 </view>
						<view class="f-caption c-caption"> 租赁面积：{{detail.area}}㎡ </view>
					</block>
				</view>
			</view>
		</view>

		<view class="fill-base mt-md pd-lg b-1px-t">
			<view class="f-paragraph c-title" v-if="options.eva_id">
				<text decode="emsp" style="word-break:break-all;">{{subForm.text || '没有填写评价哦'}}</text>
			</view>
			<block v-else>
				<textarea
					v-on:input="subForm.text = subForm.text.length > 200 ? subForm.text.substring(0,200) : subForm.text"
					v-model="subForm.text" type="text" class="item-textarea" maxlength="200"
					placeholder-class="c-placeholder" placeholder="商品满足您的期待吗？快来分享分享吧"></textarea>
				<view class="mt-md f-caption c-placeholder text-right">
					{{`已输入${subForm.text.length}/200`}}
				</view>
			</block>
		</view>
		<view class="fill-base pt-sm pl-lg pr-lg pb-lg b-1px-t"
			v-if="!options.eva_id || (options.eva_id && subForm.imgs.length > 0)">
			<upload @upload="imgUpload" @del="imgUpload" :imagelist="subForm.imgs" imgtype="imgs" text="添加图片"
				:imgsize="9" :imgauth="options.eva_id ? false : true">
			</upload>
		</view>
		<view class="fill-base mt-md pd-lg b-1px-tb">
			<view class="pb-md f-title text-bold">商品的评价</view>
			<view class="flex-center">
				<view class="flex-warp flex-1">
					<block v-for="(item,index) in 5" :key="index">
						<i @tap="checkStar" :data-num="index*1+1" class="iconfont icon-star-fill mr-sm"
							:class="[{'icon-font-color cur':subForm.star>=index*1+1 }]"></i>
					</block>
				</view>
				<view class="f-paragraph c-caption" v-if="options.eva_id">{{create_time_text}}</view>
			</view>
		</view>

		<view class="space-footer"></view>
		<view @tap.stop="toDelSubmit" class="confirm-btn ml-lg mr-lg flex-center f-title c-base radius-16"
			:style="{background:options.eva_id ?subColor:primaryColor}">{{options.eva_id ? '删除':'提交'}}</view>
		<view class="space-footer"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				scrollTop: 0,
				typeText: {
					'claim': 1,
					'land': 2,
					'shop': 3
				},
				options: {},
				detail: {},
				create_time_text: '',
				subForm: {
					star: 0,
					text: '',
					imgs: [],
				},
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
		}),
		onLoad(options) {
			this.options = options
			this.$util.showLoading()
			this.initIndex()
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		methods: {
			...mapMutations(['updateOrderItem']),
			async initIndex() {
				let {
					id,
					type,
					eva_id = 0
				} = this.options
				if (eva_id) {
					let data = await this.$api.mine.evaluateInfo({
						id: eva_id
					})
					data.imgs = data.imgs && data.imgs.length > 0 ? data.imgs.map(item => {
						return {
							path: item
						}
					}) : []
					for (let key in this.subForm) {
						this.subForm[key] = data[key]
					}
					this.create_time_text = this.$util.formatTime(data.create_time * 1000)
				}
				this.detail = await this.$api[type].orderInfo({
					id
				})
				this.$util.hideAll()
			},
			initRefresh() {
				this.initIndex()
			},
			checkStar(e) {
				let {
					num
				} = this.$util.getDataSet(e)
				this.subForm.star = num;
			},
			imgUpload(e) {
				let {
					imagelist,
					imgtype
				} = e;
				this.subForm[imgtype] = imagelist;
			},
			async toDelSubmit() {
				let {
					typeText
				} = this
				let {
					id: order_id,
					type: method,
					eva_id = 0
				} = this.options
				let {
					order_code,
					farmer_id,
					store_id = 0
				} = this.detail
				if(method === 'shop'){
					farmer_id = store_id
				}
				let subForm = this.$util.deepCopy(this.subForm)
				if (!eva_id) {
					if (!this.subForm.star) {
						this.$util.showToast({
							title: `请选择星级评价`
						})
						return
					}
					subForm.imgs = subForm.imgs.length > 0 ? subForm.imgs.map(item => {
						return item.path
					}) : []
				}
				let param = eva_id ? {
					id: eva_id,
					status: -1
				} : Object.assign({}, subForm, {
					order_id,
					order_code,
					farmer_id,
					type: typeText[method]
				})
				if (this.lockTap) return
				this.lockTap = true
				this.$util.showLoading()
				let methodModel = eva_id ? 'evaluateUpdate' : 'evaluateAdd'
				try {
					await this.$api.mine[methodModel](param)
					this.$util.hideAll()
					this.$util.showToast({
						title: eva_id ? `删除成功` : `提交成功`
					});
					this.updateOrderItem({
						key: 'haveOperItem',
						val: true
					})
					setTimeout(() => {
						this.$util.back()
						this.$util.goUrl({
							url: 1,
							openType: 'navigateBack'
						})
					}, 1000)
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			}
		}
	}
</script>


<style lang="scss">
	.mine-evaluate-edit {

		.item-textarea {
			width: 690rpx;
			overflow-y: auto;
		}

		.iconfont {
			font-size: 42rpx;
			color: #eee;
		}

		.cur {
			background-image: -webkit-linear-gradient(90deg, #FDCD47, #FFC000);
		}

		.confirm-btn {
			width: 690rpx;
			height: 96rpx;
		}
	}
</style>
